package com.manor.main.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("RestrictedApi")
public class MAbsLayout extends ViewGroup {

    public MAbsLayout(Context context) {
        super(context);
    }

    public MAbsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MAbsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onDragEvent(DragEvent event) {
        int x= (int) (event.getX()-getWidth());
        int y = (int) (event.getY() - getHeight());
        return super.onDragEvent(event);

    }

    @Override
    protected void onAnimationEnd() {
        super.onAnimationEnd();

    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return super.onStartNestedScroll(child, target, nestedScrollAxes);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //子视图数
        int count = getChildCount();

        int maxHeight = 0;
        int maxWidth = 0;

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            if (childView.getVisibility()!=View.GONE){
                MAbsLayout.LayoutParams lp = childView.getLayoutParams();
                int childRight = lp.width + childView.getMeasuredWidth();
                int childBottom = lp.height + +childView.getMeasuredHeight();
                maxWidth = Math.max(maxWidth , childRight);
                maxHeight = Math.max(maxHeight , childBottom);

                //边界检测
                int lastLeft = childRight;
                int lastTop = childBottom;
                if (childBottom - childView.getWidth()>0)
                    lastLeft =childView.getWidth();
            }
        }

        maxWidth += getPaddingLeft()+getPaddingRight();
        maxHeight += getPaddingTop()+getPaddingBottom();

        maxHeight = Math.max(maxHeight , getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth , getSuggestedMinimumWidth());
        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
                resolveSizeAndState(maxHeight, heightMeasureSpec, 0));

    }




    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int count =  getChildCount();

        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            if (childView.getVisibility() != View.GONE) {
                MAbsLayout.LayoutParams lp =childView.getLayoutParams();
                int childLeft = getPaddingLeft() + lp.width;
                int childTop = getPaddingTop() + lp.height;

                childView.layout(childLeft,childTop,childLeft+childView.getMeasuredWidth(),childTop+childView.getMeasuredHeight());
            }
        }

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


    }

//    @Override
//    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
//        return new MAbsLayout.LayoutParams(MAbsLayout.LayoutParams.WRAP_CONTENT, MAbsLayout.LayoutParams.WRAP_CONTENT, 0, 0);
//    }

    //气泡边界检测

    public void  initView(){

    }


}
